<?php
require 'header.php';
?>
 <script type = "text/javascript">
 	function change() {
 		document.getElementById("myForm").submit();
 	}
 </script>
 <?php

 
 
 function carForm($con) {
	if(!isset($_POST["make"]) or ($_POST["make"] == "default")) {
		$make = "----Select Make----";
	}
	else
		$make = $_POST["make"];				
	echo 'Make: <select name="make" onchange="change()">
		<option value="' . $make . '">' . $make . '</option>';
	$query = "SELECT distinct make FROM car WHERE car_id!=-1";
	if ($result = mysqli_query($con, $query)) {
		if (mysqli_num_rows($result) > 0) {
			while ($row = mysqli_fetch_array($result)) {
				echo '<option value="' . $row["make"] . '">' . $row["make"] . '</option>';
			}
		}
	}
	echo '</select>';

	if(!isset($_POST["make"]) or ($_POST["make"] == "default")) {

		$model = "----Select Make----";
	}
	else if (!isset($_POST["model"]) or ($_POST["model"] == "default")) {
		$model = "----Select Model----";
	}
	else
		$model = $_POST["model"];

	echo '<br>Model: <select name="model" onchange="change()">';
	echo '<option value="' . $model . '">' . $model . '</option>';
	$query = "SELECT distinct model FROM car WHERE make = '" . $make . "'";
	if ($result = mysqli_query($con, $query)) {
		if (mysqli_num_rows($result) > 0) {
			while ($row = mysqli_fetch_array($result)) {
				echo '<option value="' . $row["model"] . '">' . $row["model"] . '</option>';
			}
		}
	}
	echo '</select>';

	if(!isset($_POST['make']) || $_POST["make"] == "----Select Make----") {
		$year = '----Select Make----';
	}
	else if (!isset($_POST["model"]) || ($_POST["model"] == "----Select Model----")) {
		$year = '----Select Model----';
	}
	else if (!isset($_POST["year"]) or ($_POST["year"] == "----Select Year----")) {
		$year = '----Select Year----';
	}
	else {
		$year = $_POST["year"];
	}

	echo '<br>Year: <select name="year" onchange="change()">';
	echo '<option value="' . $year . '">' . $year . '</option>';
	$query = "SELECT distinct year FROM car WHERE make = '" . $make . "' AND model = '" . $model . "'";
	if ($result = mysqli_query($con, $query)) {
		if (mysqli_num_rows($result) > 0) {
			while ($row = mysqli_fetch_array($result)) {
				echo '<option value="' . $row["year"] . '">' . $row["year"] . '</option>';
			}
		}
	}
	echo '</select>'; 

	return;

}




if(!isset($_POST['turnin']))
{


?>
<h3>Add New Item</h3>
<form action="" method="post" id='myForm'> <!--form to add new item -->
        <table>
             <tr>
                <td>Part Number:</td>
            </tr>
            <tr>
                <td><input type="text" name="part_number" value=""></td>
            </tr>
            <tr>
                <td>Name:</td>
            </tr>
            <tr>
                <td><input type="text" name="name" value=""></td>
            </tr>
			<tr>
                <td>Price:</td>
            </tr>
            <tr>
                <td><input type="text" name="price" value=""></td>
            </tr>
			<tr>
                <td>Category:</td>
            </tr>
            <tr>
                <td><input type="car" name="category" value=""></td>
            </tr>
			<tr>
                <td>Type:</td>
            </tr>
			<tr>
                <td><input type="text" name="type" value=""></td>
            </tr>
			    <td>Manufacturer:</td>
            </tr>
			<tr>
                <td><input type="text" name="manufacturer" value=""></td>
            </tr>
			
			<tr>
				<td>Compatible Car:</td>
            </tr>
			<tr>
               <td>	<?php carForm($con);
				if (isset($_POST['make']) && isset($_POST['model']) && $_POST['year'] != "----Select Make----") 
				{
					echo "<input type='hidden' name='term' value='set'>";
				}
				?>
			</td>
            </tr>
			<tr>
				<td>Refurbished:</td>
            </tr>
			<tr>
               <td><input type="checkbox" name="refurbished" value=""></td>
            </tr>
		          
            <tr>
                <td><input type="submit" name="turnin" value="Add New Item"></td>
            </tr>

        </table>
    </form>
	<?php
	} else
	{
	if(isset($_POST['refurbished']))
		$query = "INSERT INTO item ( part_no, name, price, category,type, car,manufacturer,refurbished) VALUES ( '$_POST[part_number]','$_POST[name]','$_POST[price]','$_POST[category]','$_POST[type]', '$_POST[car]', '$_POST[manufacturer]', '1')";
	else
		$query = "INSERT INTO item ( part_no, name, price, category,type, car,manufacturer,refurbished) VALUES ( '$_POST[part_number]','$_POST[name]','$_POST[price]','$_POST[category]','$_POST[type]', '$_POST[car]', '$_POST[manufacturer]', '0')";
	if (!mysqli_query($con,$query))
	{
	die('Error: ' . mysqli_error($con));
	}
	$query="SELECT name FROM branch";
	$result=mysqli_query ($con, $query);
	while($branch = mysqli_fetch_array($result))
	{
		$query = "INSERT INTO stock (branch_name,itemID,quantity) VALUES ('$branch[name]','$_POST[part_number]','0')";

		if (!mysqli_query($con,$query))
		{
		die('Error: ' . mysqli_error($con));
		}
	}

	echo '<h3>Your item was added<h3>';	
	}
	
	require 'footer.php';					
	?>
	